home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / dialog / kbd.c < prev    next >
C/C++ Source or Header  |  1996-03-09  |  2KB  |  115 lines

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/time.h>
  4. #include "dialog.h"
  5. #include "diadef.h"
  6.  
  7. static int timeout_sec=0;
  8. static MENU_STATUS keyret;
  9. static bool reactivate = false;
  10. /*
  11.     Specify the timeout for the next input in seconds.
  12.     The timeout reset itself. It means that the first key pressed
  13.     by the user will desactivated the timeout feature until
  14.     dialog_settimeout() is called again. This function is generally
  15.     called only once at the beginning of a program which must
  16.     continue by itself if the user do not interact (there is no user).
  17.  
  18.     If the timeout expire before a key is depressed, retcod is returned.
  19.     retcod is generally MENU_ESCAPE or something equivalent.
  20. */
  21. void dialog_settimeout(
  22.     int nbsec,
  23.     MENU_STATUS retcod,
  24.     bool rearm)        // Reactivate itself a each input
  25. {
  26.     timeout_sec = nbsec;
  27.     keyret = retcod;
  28.     reactivate = rearm;
  29. }
  30.  
  31. static int current_timeout_sec;
  32.  
  33. /*
  34.     Initialise the timeout before the first call to dialog_wgetch().
  35. */
  36. void dialog_starttimeout()
  37. {
  38.     current_timeout_sec = timeout_sec;
  39. }
  40.  
  41. int dialog_getcurtimeout()
  42. {
  43.     return current_timeout_sec;
  44. }
  45.  
  46. /*
  47.     Wait for a char and check the timeout.
  48.     If the timeout is active, terminate every seconds to allow
  49.     the dialog editor to take control once in a while
  50. */
  51. int dialog_wgetch(WINDOW *w, MENU_STATUS &timeout_button)
  52. {
  53.     int ret;
  54.     wrefresh(w);
  55.     doupdate();
  56.     while (1){
  57.         int quit_now = 0;
  58.         if (current_timeout_sec != 0){
  59.             fd_set set;
  60.             struct timeval out;
  61.             FD_ZERO (&set);
  62.             FD_SET  (0,&set);
  63.             out.tv_sec = 1;
  64.             current_timeout_sec--;
  65.             out.tv_usec = 0;
  66.             if (select(1,&set,NULL,NULL,&out) > 0){        
  67.                 ret = wgetch(w);
  68.                 current_timeout_sec = 0;
  69.             }else{
  70.                 ret = 0;
  71.                 if (current_timeout_sec == 0){
  72.                     // This is the final timeout
  73.                     timeout_button = keyret;
  74.                 }else{
  75.                     // This is a stop gap one second timeout
  76.                     timeout_button = MENU_INTERNAL_TIMEOUT;
  77.                 }
  78.                 quit_now = 1;
  79.             }        
  80.             if (!reactivate) timeout_sec = 0;
  81.         }else{
  82.             ret = wgetch(w);
  83.         }
  84.         if (!quit_now && ret == 0x1b){
  85.             fd_set set;
  86.             struct timeval out;
  87.             FD_ZERO (&set);
  88.             FD_SET  (0,&set);
  89.             out.tv_sec = 0;
  90.             out.tv_usec = 10000;
  91.             if (wgetch(w) != 0x1b){
  92.                 ret = wgetch(w);
  93.                 if (ret == 0x32){
  94.                     ret = INS;
  95.                     break;
  96.                 }
  97.             }else{
  98.                 /* It was really an escape alone */
  99.                 break;
  100.             }
  101.         }else{
  102.             break;
  103.         }
  104.     }
  105.     #if 0
  106.     {
  107.         FILE *fout = fopen ("/tmp/dbg","a");
  108.         fprintf (fout,"ret = %x %o %d\n",ret,ret,ret);
  109.         fclose (fout);
  110.     }
  111.     #endif
  112.     return ret;
  113. }
  114.  
  115.